#!/usr/bin/env python
from math import sin, pi
from time import time as getTime

class BehaviorHandler:
    def __init__(self):
        self.behaviors = set()
        self.time = getTime()
    
    def update(self):
        self.time = t = getTime()
        toRemove = set()
        for behavior in self.behaviors:
            if t < behavior.startTime + behavior.duration:
                behavior.update(t)
            else:
                behavior.update(behavior.startTime + behavior.duration)
                toRemove.add(behavior)
        
        for behavior in toRemove:
            self.unregister(behavior)
    
    def register(self, behavior):
        self.behaviors.add(behavior)
    
    def unregister(self, behavior):
        self.behaviors.remove(behavior)

class Behavior:
    def __init__(self, root, duration):
        root.register(self)
        
        self.startTime = root.time
        self.duration = duration

class Sinterp(Behavior):
    def __init__(self, root, object, var, start, end, duration=500.0):
        Behavior.__init__(self, root, duration)
        self.object = object
        self.var = var
        self.startVal = start
        self.deltaVal = end - start
    
    def update(self, time):
        t = (time - self.startTime) / self.duration
        val = self.startVal + self.deltaVal * sin(pi / 2.0 * t)
        self.object.__setattr__(self.var, val)